package com.jenkins.test.proxy.gov.business;

import com.alibaba.fastjson.JSONObject;
import com.jenkins.test.proxy.gov.dto.GovRequest;
import com.jenkins.test.proxy.gov.dto.GovResponse;
import com.jenkins.test.utils.HttpsUtil;
import com.jenkins.test.proxy.gov.constant.GovConstant;
import com.jenkins.test.proxy.gov.constant.GovStandardSigmoid;
import lombok.extern.slf4j.Slf4j;

/**
 * @version V1.0.0
 * @ClassName: {@link DefaultExecutor}
 * @Description: DefaultExecutor
 * @author: Lanzhou
 * @date: 2022/1/22 14:55
 * @Copyright:2020 All rights reserved.
 */
@Slf4j
public class DefaultExecutor extends HttpsUtil {

    /**
     * 默认请求执行器
     *
     * @param request 请求
     * @return 返回
     */
    public Object execute(GovStandardSigmoid sigmoid, GovRequest request) {
        String json = super.call(request.getRequestType(), request.getRequestUrl(), request.getBody(), request.getHeaders());
        GovResponse response = null;
        try {
            response = JSONObject.parseObject(json, GovResponse.class);
        } catch (Exception e) {
            String warnMsg = "源数据: " + json + " \n 调用描述: " + sigmoid.getDescribe();
            log.warn("数据转换失败, 具体信息为: \n [{}]", warnMsg);
            return JSONObject.parseObject("{\"responseCode\": \"E500\",\"responseMessage\": \"" + warnMsg + "\"}", sigmoid.getRepType());
        }
        if (response.getResponseCode().equals(GovConstant.SUCCESS_CODE)) {
            // 根据我们的类型值 - sigmoid 找到对应的类信息作为转换依据
            Object data = response.getData();
            // data内部的信息转换
            String dataJson = JSONObject.toJSONString(data);
            return JSONObject.parseObject(dataJson, sigmoid.getRepType());
        }
        return JSONObject.parseObject("{\"responseCode\": \"E500\", \"responseMessage\": \"未知异常, 请检查参数日志\"}", sigmoid.getRepType());
    }


}
